
const connection = require("../app/database");

class MomentServices {
  async create(userId, content) {
    // 数据库处理
    const statement = `INSERT INTO moment (user_id, content) VALUES (?, ?)`
    const result = await connection.execute(statement, [userId, content])
    return result
  }

  async getMomentDetail(id) {
    // 查询对应id 数据详情
    const statement = `SELECT 
    m.id id, m.content content, m.createTime createTime, m.updataTime updataTime,
    JSON_OBJECT('user_id', u.id, 'user_name', u.name) author
    FROM moment m LEFT JOIN users u ON u.id = m.user_id
    WHERE m.id = ?`;
    const res = await connection.execute(statement, [id]);
    return res[0];
  }

  async getServicesList(limit, total) {
    const statement = `
    SELECT 
    m.id id, m.content content, m.createTime createTime, m.updataTime updataTime,
    JSON_OBJECT('user_id', u.id, 'user_name', u.name) user
    FROM moment m LEFT JOIN users u ON u.id = m.user_id
    LIMIT ?, ?
    `;
    const res = await connection.execute(statement, [limit, total]);
    console.log(res[0], "res")
    return res[0]
  }

  // 根据moment_id 去跟新品论
  async updateServicesMonent(id, content) {
    console.log(id, content);
    const statement = `
    UPDATE moment SET content = ? WHERE id = ?;
    `;
    const res =await connection.execute(statement, [content, id])
    return res[0]
  }

  // 删除评论
  async removeServicesMonent(id) {
    console.log(id, "id")
    const statement = `
    DELETE FROM moment WHERE id = ?;
    `;
    const res = await connection.execute(statement, [id])
    return res[0]
  }
};


module.exports = new MomentServices()